/*
 * 文章
 */

import { createAPI } from '@/utils/request'

// 文章列表
// export const getArticlesList = data => createAPI('/articles', 'get', data)
export const getArticlesList = function(data) {
  const stateMapping = new Map([
    [1, '启用'],
    [0, '禁用'],
  ])
  return createAPI('/articles', 'get', data)
    .then(res => ({
      counts: res.data.counts,
      items: res.data.items.map((d, i) => ({
        ...d,
        createTime: d.createTime.substring(0, 19).replace('T', ' '),
        index: (data.page - 1) * data.pagesize + i + 1,
        state: stateMapping.get(d.state),
      })),
    }))
    .catch(err => err)
}

// 文章简单列表
export const simple = data => createAPI('/articles/simple', 'get', data)

// 单个文章详情
export const articlesDetail = data => createAPI(`/articles/${data.id}`, 'get', data)

// 文章添加
export const addArticles = data => createAPI('/articles', 'post', data)

// 文章更新
export const updateArticles = data => createAPI(`/articles/${data.id}`, 'put', data)

// 文章删除
export const removeArticles = data => createAPI(`/articles/${data.id}`, 'delete', data)

// 文章状态
export const changeState = data =>
  createAPI(`/articles/${data.id}/${data.state}`, 'post', data)
